package org.heart.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.heart.entity.dto.FriendRelationship;
import org.heart.entity.dto.UserBaseInfo;
import org.heart.mapper.FriendRelationshipMapper;
import org.heart.mapper.UserBaseInfoMapper;
import org.heart.entity.resp.FriendGroupListResp;
import org.heart.service.IFriendRelationshipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 好友信息关联表 服务实现类
 * </p>
 *
 * @author lml
 * @since 2025-06-29
 */
@Slf4j
@Service
public class FriendRelationshipServiceImpl extends ServiceImpl<FriendRelationshipMapper, FriendRelationship> implements IFriendRelationshipService {

    @Resource
    private UserBaseInfoMapper lUserBaseInfoMapper;

    @Resource
    private FriendRelationshipMapper friendRelationshipMapper;

    @Override
    public List<FriendGroupListResp> getFriendGroups(String userId,String nickName) {

        log.info("需要查询用户 UserId={}，好友类表接口。",userId);

        QueryWrapper<FriendRelationship> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);

        List<FriendRelationship> list = friendRelationshipMapper.selectList(qw);

        Map<String,List<FriendRelationship>> map = new HashMap<>();

        for(FriendRelationship item: list){

            String key = item.getRelationType();
            if(!StringUtils.isEmpty(key)){
                if(map.containsKey(key)){
                    List<FriendRelationship> lists = map.get(key);
                    lists.add(item);
                    map.put(key, lists);
                }else{
                    List<FriendRelationship> lists = new ArrayList<>();
                    lists.add(item);
                    map.put(key,lists);
                }
            }
        }

        List<FriendGroupListResp> friends = new ArrayList<>();

        map.forEach((key, value) -> {
            List<UserBaseInfo> list2 = new ArrayList<>();
            value.forEach(relationship -> {
                QueryWrapper<UserBaseInfo> qw2 = new QueryWrapper<>();
                qw2.eq("user_id",relationship.getFriendId());
                if(!StringUtils.isEmpty(nickName)){
                    qw2.like("nickname",nickName);
                }
                UserBaseInfo userInfo = lUserBaseInfoMapper.selectOne(qw2);
                if(ObjectUtil.isNotNull(userInfo)){
                    list2.add(userInfo);
                }
            });
            FriendGroupListResp friendGroupListResp = new FriendGroupListResp();
            friendGroupListResp.setFriends(list2);
            friendGroupListResp.setRelationType(key);
            friends.add(friendGroupListResp);
        });

        return friends;
    }

    @Override
    public List<FriendRelationship> getFriendList(String userId) {

        log.info("需要查询用户 UserId={}，好友类表接口。",userId);

        QueryWrapper<FriendRelationship> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);

        List<FriendRelationship> list = friendRelationshipMapper.selectList(qw);

        return list;
    }
}
